/**
* <p>Title: PushService.java</p>
* <p>Copyright: Copyright (c) 2017</p>
* <p>Company: lty</p>
*/
package com.lantaiyuan.ebus.task;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.lantaiyuan.ebus.model.PhonePlatEntity;
import com.lantaiyuan.ebus.model.PushPlatformGroup;
import com.lantaiyuan.ebus.util.JpushHelper;

/**
* <p>Title: PushService</p>
* <p>Description: 消息推送业务实现类</p>
* <p>Company: lty</p>
* @author liuhao
* @date 2017年3月13日 下午3:01:26
*/
@Component
public class PushTask{
	private static final Logger logger = LoggerFactory.getLogger(PushTask.class);
	@Autowired
    private RestTask restUtil;
	
	@Async("myAsync")
	public void pushMsg(String jsonString) {
		logger.warn("异步调用业务方法进入");
		// 解析进出站包、gps包
		/********************* 泰国警报推送业务代码新增 ********************/
		// 解析报警异常信息
		// 所有
		List<PushPlatformGroup> allRes = restUtil.invokeUrl();

		// 所有
		List<PhonePlatEntity> all = null;
		// 异常开关门
		List<PhonePlatEntity> openCloseDoors = null;
		// 离线
		List<PhonePlatEntity> leaveLines = null;
		// 超速
		List<PhonePlatEntity> overSpeeds = null;
		// 疑似抛锚
		List<PhonePlatEntity> breakDowns = null;
		// 超载
		List<PhonePlatEntity> overloadAlarms = null;
		// 紧急报警
		List<PhonePlatEntity> emergencyAlarms = null;

		for (PushPlatformGroup pushPlatformGroup : allRes) {
			if (pushPlatformGroup.getFlag() == 0) {
				all = pushPlatformGroup.getPhonePlats();
			} else if (pushPlatformGroup.getFlag() == 1) {
				openCloseDoors = pushPlatformGroup.getPhonePlats();
			} else if (pushPlatformGroup.getFlag() == 2) {
				leaveLines = pushPlatformGroup.getPhonePlats();
			} else if (pushPlatformGroup.getFlag() == 3) {
				overSpeeds = pushPlatformGroup.getPhonePlats();
			} else if (pushPlatformGroup.getFlag() == 4) {
				breakDowns = pushPlatformGroup.getPhonePlats();
			} else if (pushPlatformGroup.getFlag() == 5) {
				overloadAlarms = pushPlatformGroup.getPhonePlats();
			} else if (pushPlatformGroup.getFlag() == 99) {
				emergencyAlarms = pushPlatformGroup.getPhonePlats();
			}
		}
		
		logger.warn("异步调用业务方法中间");
		
		JSONObject jsonObj = JSONObject.parseObject(jsonString);

		String type = null;
		String lineName = null;
		String carName = null;
		String occurDate = null;

		// 修订警报描述
		type = alarmTypeRevise(jsonObj.getString("type"));
		lineName = jsonObj.getString("lineName");
		carName = jsonObj.getString("carName");

		Date date = jsonObj.getDate("occurDate");
		occurDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

		StringBuilder sb = new StringBuilder();
		sb.append("{\"msg_content\": \"").append("Busline:").append(lineName).append(",").append("Vehicle number:")
				.append(carName).append(",").append("Time:").append(occurDate).append(" trigger ").append(type)
				.append(" alarm!").append("\",\"title\": \"").append(type).append("\"}");
		String alarmMsg = sb.toString();

		StringBuilder sb2 = new StringBuilder();
		sb2.append("Busline:").append(lineName).append(",").append("Vehicle number:").append(carName).append(",")
				.append("Time:").append(occurDate).append(" trigger ").append(type).append(" alarm!");
		String alarmMsg2 = sb2.toString();
		
		logger.warn("异步调用业务方法开发发送");
		// 向设置所有推送消息
		jpush2Group(all, type, alarmMsg, alarmMsg2);

		// 向自定义设置推送消息
		switch (type) {
		case "off route":
			jpush2Group(leaveLines, type, alarmMsg, alarmMsg2);
			break;
		case "overtime parking":
			jpush2Group(breakDowns, type, alarmMsg, alarmMsg2);
			break;
		case "overspeed":
			jpush2Group(overSpeeds, type, alarmMsg, alarmMsg2);
			break;
		case "abnormal door open":
			jpush2Group(openCloseDoors, type, alarmMsg, alarmMsg2);
			break;
		case "overload":
			jpush2Group(overloadAlarms, type, alarmMsg, alarmMsg2);
			break;
		case "SOS alarm":
			jpush2Group(emergencyAlarms, type, alarmMsg, alarmMsg2);
			break;
		default:
			break;
		}
		logger.warn("异步调用业务方法结束");
	}

	/**
	 * <p>
	 * Title: 给用户组发送消息推送
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 */
	private void jpush2Group(List<PhonePlatEntity> phonePlats, String title, String msgContent, String msgContent2) {
		logger.warn("jpush2Group方法调用开始");
		if (!CollectionUtils.isEmpty(phonePlats)) {
			phonePlats.forEach(g -> {
				if (g.getType() == 1 && !StringUtils.isEmpty(g.getRegId())) {
					JpushHelper.sendMessageAndroidWithExtra(title, msgContent, g.getRegId());
				} else if (g.getType() == 2 && !StringUtils.isEmpty(g.getRegId())) {
					JpushHelper.sendNotifyIOSWithExtra(title, msgContent2, g.getRegId());
				}
			});
		}
		logger.warn("jpush2Group方法调用结束");
	}

	// 修订警报提示
	private String alarmTypeRevise(String type) {
		logger.warn("alarmTypeRevise方法调用开始");
		switch (type) {
		case "leaveLine":
			return "off route";
		case "breakDown":
			return "overtime parking";
		case "overSpeed":
			return "overspeed";
		case "openCloseDoor":
			return "abnormal door open";
		case "overLoad":
			return "overload";
		case "emergencyAlarm":
			return "SOS alarm";
		default:
			return "";
		}
	}

	/*************************** end ***************************/
}
